package omg.ligong.action;

import java.io.PrintWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import omg.ligong.dao.CollegeDAO;
import omg.ligong.dao.DAOFactory;
import omg.ligong.dao.impl.CollegeDAOImpl;
import omg.ligong.domain.College;
import omg.ligong.util.StrFunc;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

public class CollegeMgrAction extends Action {
	private final static String CMD_LIST_ALL = "listall";// 列出所有类型
	private final static String CMD_ADD = "add";
	private final static String CMD_DEL = "del";
	private final static String CMD_EDIT = "modify";
	private final static String CMD_COUNT = "count";
	private final static String CMD_GETDETAIL = "getdetail";
	private final static int PAGE_SIZE = 40;// 每页显示40条
	private final static String DATE_FORMATE = "yyyy-MM-dd";

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String cmd = request.getParameter("cmd");
		if (CMD_LIST_ALL.equals(cmd)) {
			return listAll(mapping, request, response);
		} else if (CMD_ADD.equals(cmd)) {
			return addCollege(mapping, request, response);
		} else if (CMD_DEL.equals(cmd)) {
			return delCollege(mapping, request, response);
		} else if (CMD_COUNT.equals(cmd)) {
			return countCollege(mapping, request, response);
		} else if (CMD_EDIT.equals(cmd)) {
			return changeCollege(mapping, request, response);
		} else if (CMD_GETDETAIL.equals(cmd)) {
			return getDetail(mapping, request, response);
		}
		return super.execute(mapping, form, request, response);
	}

	private ActionForward getDetail(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String collegename = request.getParameter("collegename");
		response.setContentType("text/html;charset=UTF-8");
		CollegeDAO dao = DAOFactory.getInstance().getCollegeManager();
		JSONObject obj = new JSONObject();
		if (collegename == null || collegename == "") {
			obj.put("error", "FAIL");
			obj.put("msg", "请选择需要修改的学院");
			PrintWriter out = response.getWriter();
			try {
				out.print(obj.toString());
				out.flush();
			} finally {
				out.close();
			}
		} else {
			collegename = java.net.URLDecoder.decode(collegename, "UTF-8");
			College college = dao.findCollegeByName(collegename);
			if (college == null) {
				obj.put("error", "FAIL");
				obj.put("msg", "该学院不存在");
				PrintWriter out = response.getWriter();
				try {
					out.print(obj.toString());
					out.flush();
				} finally {
					out.close();
				}
			} else {
				obj.put("collegename", college.getName());
				obj.put("desc", StrFunc.isNull(college.getDescription()) ? ""
						: college.getDescription());
				obj.put("enable", college.isEnable() ? "1" : "0");
				PrintWriter out = response.getWriter();
				try {
					out.print(obj.toString());
					out.flush();
				} finally {
					out.close();
				}
			}
		}
		return null;
	}

	private ActionForward changeCollege(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String src = request.getParameter("src");
		String dest = request.getParameter("dest");
		String desc = request.getParameter("desc");
		String enable = request.getParameter("enable");
		if (StrFunc.isNull(dest)) {
			JSONObject obj = new JSONObject();
			obj.put("error", "FAIL");
			obj.put("msg", "请输入修改后的课程类型");
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			try {
				out.print(obj.toString());
				out.flush();
			} finally {
				out.close();
			}
		} else {
			JSONObject obj = new JSONObject();
			src = java.net.URLDecoder.decode(src, "UTF-8");
			dest = java.net.URLDecoder.decode(dest, "UTF-8");
			desc = java.net.URLDecoder.decode(desc, "UTF-8");
			CollegeDAO dao = DAOFactory.getInstance().getCollegeManager();
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			try {
				dao.modifyCollege(src, dest, desc, "1".equals(enable));
				out.print(obj.toString());
			} catch (Exception e) {
				obj.put("error", "FAIL");
				obj.put("msg", e.getMessage());
				out.print(obj.toString());
			} finally {
				out.close();
			}
		}
		return null;
	}

	private ActionForward countCollege(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CollegeDAO dao = DAOFactory.getInstance().getCollegeManager();
		int count = dao.countCollege();
		response.setContentType("text/html;charset=gbk");
		PrintWriter out = response.getWriter();
		try {
			out.print(count);
			out.flush();
		} finally {
			out.close();
		}
		return null;
	}

	private ActionForward delCollege(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String collegename = request.getParameter("collegename");
		if (collegename == null || collegename.equals("")) {
			JSONObject obj = new JSONObject();
			obj.put("error", "FAIL");
			obj.put("msg", "请选择要删除的学院名");
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			try {
				out.print(obj.toString());
				out.flush();
			} finally {
				out.close();
			}
		} else {
			JSONObject obj = new JSONObject();
			collegename = java.net.URLDecoder.decode(collegename, "UTF-8");
			CollegeDAO dao = DAOFactory.getInstance().getCollegeManager();
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			try {
				dao.delCollege(collegename);
				out.print(obj.toString());
			} catch (Exception e) {
				obj.put("error", "FAIL");
				obj.put("msg", e.getMessage());
				out.print(obj.toString());
			} finally {
				out.close();
			}
		}
		return null;
	}

	private ActionForward addCollege(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String collegename = request.getParameter("collegename");
		String desc = request.getParameter("desc");
		String enable = request.getParameter("enable");
		if (collegename == null || collegename.equals("")) {
			JSONObject obj = new JSONObject();
			obj.put("error", "FAIL");
			obj.put("msg", "请输入增加的课程类型");
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			try {
				out.print(obj.toString());
				out.flush();
			} finally {
				out.close();
			}
		} else {
			JSONObject obj = new JSONObject();
			collegename = java.net.URLDecoder.decode(collegename, "UTF-8");
			desc = StrFunc.isNull(desc) ? "" : java.net.URLDecoder.decode(desc,
					"UTF-8");

			CollegeDAO dao = DAOFactory.getInstance().getCollegeManager();
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			try {
				dao.addCollege(new College(-1, collegename, desc, "1"
						.equals(enable) ? true : false, new Date(System
						.currentTimeMillis())));
				out.print(obj.toString());
			} catch (Exception e) {
				obj.put("error", "FAIL");
				obj.put("msg", e.getMessage());
				out.print(obj.toString());
			} finally {
				out.close();
			}
		}
		return null;
	}

	private ActionForward listAll(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int currentpage = 0;
		String page = request.getParameter("page");
		if (StrFunc.isNull(page)) {
			currentpage = 1;
		} else {
			currentpage = Integer.parseInt(page);
		}
		CollegeDAO dao = DAOFactory.getInstance().getCollegeManager();
		List list = dao.listCollege((currentpage - 1) * PAGE_SIZE, PAGE_SIZE);
		response.setContentType("text/html;charset=gbk");
		if (list == null || list.isEmpty()) {
			PrintWriter out = response.getWriter();
			try {
				JSONObject obj = new JSONObject();
				JSONArray arrary = new JSONArray();
				obj.put("total", "0");
				obj.put("rows", arrary);
				out.print(obj.toString());
				out.flush();
			} finally {
				out.close();
			}
		} else {
			JSONArray array = new JSONArray();
			for (int i = 0; i < list.size(); i++) {
				College type = (College) list.get(i);
				JSONObject obj = new JSONObject();
				obj.put(CollegeDAOImpl.FIELD_NAME, type.getName());
				obj.put(CollegeDAOImpl.FIELD_DATE, date2Str(type
						.getCreateDate()));
				obj.put(CollegeDAOImpl.FIELD_ENABLE, type.isEnable() ? "可用"
						: "不可用");
				array.put(i, obj);
			}
			PrintWriter out = response.getWriter();
			try {
				JSONObject obj = new JSONObject();
				obj.put("total", 100);
				obj.put("rows", array);
				out.print(obj.toString());
				out.flush();
			} finally {
				out.close();
			}
		}
		return null;
	}

	public static String date2Str(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMATE);
		return format.format(date);
	}
}
